package com.itboye.lottery.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;

import com.itboye.lottery.R;
import com.itboye.lottery.bean.AddressBean;
import com.itboye.lottery.utils.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by  HONGDA on 2018/6/19.
 */
public class AddressListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Activity context;
    public List<AddressBean.AddressItem> mData = new ArrayList<>();
    private LayoutInflater inflater;

    public OnItemClickListener getOnItemClickListener() {
        return onItemClickListener;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    OnItemClickListener onItemClickListener;

    public AddressListAdapter(Activity context, List<AddressBean.AddressItem> mData) {
        this.context = context;
        this.mData = mData;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_address, parent, false);
        AddressListViewHolder holder = new AddressListViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        AddressListViewHolder viewHolder = (AddressListViewHolder) holder;
        if (mData != null) {
            AddressBean.AddressItem addressBean = mData.get(position);
            viewHolder.tvPersonName.setText(addressBean.getConsignee());
            viewHolder.tv_item_address_detail.setText(addressBean.getProvince() + addressBean.getCity() + addressBean.getDistrict() + addressBean.getAddress());

            if (addressBean.getIs_default() == 1) {
                viewHolder.addressRadio.setBackgroundResource(R.mipmap.morendizhixuanzhong);
            } else {
                viewHolder.addressRadio.setBackgroundResource(R.mipmap.morendizhiweixuanzhong);
            }
        }
//        holder.textView.setText(str);
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class AddressListViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        TextView tvPersonName, tv_item_address_detail;
        ImageView addressRadio;//是否为默认
        LinearLayout llDelete;//删除
        LinearLayout llRevice;//编辑
        View view;

        public AddressListViewHolder(View itemView) {
            super(itemView);
            view = itemView;
            llRevice = itemView.findViewById(R.id.llRevice);
            llDelete = itemView.findViewById(R.id.llDelete);

            tvPersonName = itemView.findViewById(R.id.tvPersonName);
            tv_item_address_detail = itemView.findViewById(R.id.tv_item_address_detail);
            addressRadio = itemView.findViewById(R.id.addressRadio);
            llRevice.setOnClickListener(this);
            llDelete.setOnClickListener(this);
            addressRadio.setOnClickListener(this);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (onItemClickListener != null) {
                onItemClickListener.onItemClick(view, getAdapterPosition());
            }
        }
    }

}
